<html>
  <head>
    <title>container on browser</title>
    <link rel="stylesheet" href="https://unpkg.com/@xterm/xterm@5.5.0/css/xterm.css" />
  </head>
  <body>
    <div id="terminal"></div>
    <script src="./load.js"></script>
    <script type="module">
      import 'https://unpkg.com/xterm@5.3.0/lib/xterm.js';
      import 'https://unpkg.com/xterm-pty/index.js';
      import './arg-module.js'
      import initEmscriptenModule from './out.js';

      const xterm = new Terminal();
      xterm.open(document.getElementById("terminal"));

      const { master, slave } = openpty();

      xterm.loadAddon(master);

      Module.pty = slave;
      Module['mainScriptUrlOrBlob'] = location.origin + "/out.js";

      // "/pack/info" file needs to be provided to the filesystem to start
      // the container. Refer to /cmd/init/main.go for the detail of info file.
      let info = "t:" + Math.round(new Date() / 1000) + "\n";
      Module['preRun'].push((mod) => {
          mod.FS.mkdir('/pack');
          mod.FS.writeFile('/pack/info', info);
      });

      (async () => {
          const instance = await initEmscriptenModule(Module);
          var oldPoll = Module['TTY'].stream_ops.poll;
          Module['TTY'].stream_ops.poll = (stream, timeout) => oldPoll.call(stream, 0);
      })();
    </script>
  </body>
</html>
